﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ERPStore.Cart.ViewModels
{
	public class CartViewModel
	{
		public string Code { get; set; }
		public DateTime CreationDate { get; set; }
		public string VisitorId { get; set; }
		public List<CartItemViewModel> Items { get; private set; }
		public string LastPage { get; set; }
		public string Message { get; set; }
		public string CustomerDocumentReference { get; set; }
		public bool AllowPartialDelivery { get; set; }
		public bool IsPresent { get; set; }

		public decimal GrossTotal
		{
			get
			{
				if (Items == null)
				{
					return 0;
				}
				return Items.Sum(i => i.CatalogTotal);
			}
		}

		/// <summary>
		/// Montant total HT du panier sans l'eco participation
		/// </summary>
		/// <value>The total.</value>
		public decimal Total
		{
			get
			{
				if (Items == null)
				{
					return 0;
				}
				return Items.Sum(i => i.Total);
			}
		}

		public decimal ItemsDiscountTotal
		{
			get
			{
				return GrossTotal - Total;
			}
		}

		/// <summary>
		/// Montant total du panier hors recyclage, port et produits remisés
		/// </summary>
		public decimal TotalWithoutDiscountByItem
		{
			get
			{
				if (Items == null)
				{
					return 0;
				}
				return Items.Where(i => i.Product.SelectedOffer == null).Sum(i => i.Total);
			}
		}

		/// <summary>
		/// Montant total Remisé
		/// </summary>
		public decimal TotalWithDiscount
		{
			get
			{
				return Math.Max(GrossTotal - DiscountTotal - ItemsDiscountTotal, 0);
			}
		}

		public decimal TotalTax
		{
			get
			{
				if (Items == null)
				{
					return 0;
				}
				return Items.Sum(i => i.TotalTax);
			}
		}

		public decimal TotalWithTax
		{
			get
			{
				long sum = Convert.ToInt64(Math.Round(this.Total * 100m));
				sum += Convert.ToInt64(Math.Round(this.TotalTax * 100m));

				return sum / 100m;
			}
		}

		/// <summary>
		/// Montant total ht de l'eco participation
		/// </summary>
		/// <value>The recycle total.</value>
		public decimal RecycleTotal
		{
			get
			{
				if (Items == null)
				{
					return 0;
				}
				return Items.Sum(i => i.RecycleTotal);
			}
		}
		/// <summary>
		/// Montant total de la tva de l'eco participation
		/// </summary>
		/// <value>The recycle tax total.</value>
		public decimal RecycleTaxTotal
		{
			get
			{
				if (Items == null)
				{
					return 0;
				}
				return Items.Sum(i => i.RecycleTaxTotal);
			}
		}
		/// <summary>
		/// Montant total ttc de l'eco participation
		/// </summary>
		/// <value>The recycle total with tax.</value>
		public decimal RecycleTotalWithTax
		{
			get
			{
				if (Items == null)
				{
					return 0;
				}
				return Items.Sum(i => i.RecycleTotalWithTax);
			}
		}

		/// <summary>
		/// Remise totale sur le panier
		/// </summary>
		public decimal DiscountTotal
		{
			get
			{
				return DiscountList.Sum(i => i.Discount);
			}
		}

		/// <summary>
		/// Taxe totale sur la remise totale
		/// </summary>
		public decimal DiscountTaxTotal
		{
			get
			{
				return DiscountList.Sum(i => i.DiscountWithTax - i.Discount);
			}
		}

		/// <summary>
		/// Remise totale avec taxe
		/// </summary>
		public decimal DiscountWithTaxTotal
		{
			get
			{
				return DiscountList.Sum(i => i.DiscountWithTax);
			}
		}

		/// <summary>
		/// Montant total ht du panier y compris l'eco participation
		/// </summary>
		/// <value>The grand total.</value>
		public decimal GrandTotal
		{
			get
			{
				return Total + RecycleTotal - DiscountTotal + ShippingFee.Total;
			}
		}
		/// <summary>
		/// Montant total de la tva y compris avec l'eco participation
		/// </summary>
		/// <value>The grand tax total.</value>
		public decimal GrandTaxTotal
		{
			get
			{
				return TotalTax + RecycleTaxTotal - DiscountTaxTotal + ShippingFee.TotalTax;
			}
		}
		/// <summary>
		/// Montant total ttc du panier y compris avec l'eco participation
		/// </summary>
		/// <value>The grand total with tax.</value>
		public decimal GrandTotalWithTax
		{
			get
			{
				return TotalWithTax + RecycleTotalWithTax - DiscountWithTaxTotal + ShippingFeeTotalWithTax;
			}
		}

		/// <summary>
		/// Montant total du port de livraison TTC
		/// </summary>
		/// <value>The shipping fee total with tax.</value>
		public decimal ShippingFeeTotalWithTax
		{
			get
			{
				return ShippingFee.TotalWithTax;
			}
		}

		/// <summary>
		/// Montant de la remise globale en pourcentage
		/// </summary>
		/// <value>The discount.</value>
		public double Discount
		{
			get
			{
				if (Total == 0)
				{
					return 0;
				}
				return Convert.ToDouble(DiscountTotal / Total);
			}
		}

		/// <summary>
		/// Frais de livraison
		/// </summary>
		/// <value>The shipping fee.</value>
		public ERPStore.Models.Fee ShippingFee { get; set; }

		/// <summary>
		/// Liste des coupons de réduction associé au panier
		/// </summary>
		public IEnumerable<ERPStore.Models.IDiscount> DiscountList { get; set; }

		/// <summary>
		/// Transporteur selectionné
		/// </summary>
		/// <value>The conveyor.</value>
		public ERPStore.Models.Conveyor Conveyor { get; set; }

		/// <summary>
		/// Message associé au transport
		/// </summary>
		public string MessageForConveyor { get; set; }
	}
}
